package org.zlb.netty.server.rpc;

import java.util.List;

import org.zlb.netty.util.ProtostuffUtil;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class RpcMessageDecode extends ByteToMessageDecoder{
    
    private Class<?> message;
    
    public RpcMessageDecode(Class<?> message) {
        this.message = message;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
         final int length = in.readableBytes();
         final byte[] data = new byte[length];
         in.readBytes(data);
         Object obj = ProtostuffUtil.deserialize(data, message);
         out.add(obj);
    }

}
